package com.fellansoft.moviescollection.activities;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.widget.*;
import com.fellansoft.moviescollection.R;
import com.fellansoft.moviescollection.activities.EditMovie;
import com.fellansoft.moviescollection.gui.ImageAdapterControlGrid;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Naty    ->Naty
 * Date: 5/12/12
 * Time: 9:02 PM
 * To change this template use File | Settings | File Templates.
 */
public class PictureControl extends Activity {

    /**
    * Grid view for pictures
    */
    private GridView gridView;
    /**
     * List with URIs to be loaded into the grid view
     */
    public static List<Uri> uriList;
    /**
     * Constant for selection a picture
     */
    private final int SELECT_PHOTO = 666;
    /**
     * Image adapter for the content of the grid
     */
    private ImageAdapterControlGrid imageAdapter;
    /**
     * View Holder for the gird view containing  an image
     */
    static class ViewHolder {
        ImageView pictureImageView;
    }
    /**
     * Button for exiting from the activity
     */
    private Button doneButton;


    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setTheme(android.R.style.Theme_Holo);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.picturecontrol);
        Bundle extras = getIntent().getExtras();
        Integer addMovieFlag=0;
        doneButton=(Button)findViewById(R.id.donePictureButt);
        doneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        if (extras != null) {
            addMovieFlag = extras.getInt("ADDMOVIE")  ;

        }
        if(addMovieFlag==0)
        {
            uriList= EditMovie.myMovie.getPhotos();
        }
        else
           if(uriList==null)
              uriList=new ArrayList<Uri>();
        gridView = (GridView) findViewById(R.id.photoGridView);
        imageAdapter = new ImageAdapterControlGrid(this, uriList);
        gridView.setAdapter(imageAdapter);
        registerForContextMenu(gridView);
   }


    public void addPhotoGrid(View view) {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, SELECT_PHOTO);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        switch (requestCode) {
            case SELECT_PHOTO:
                if (resultCode == RESULT_OK) {
                    Uri selectedImage = imageReturnedIntent.getData();
                    InputStream imageStream = null;
                    try {

                        imageStream = getContentResolver().openInputStream(selectedImage);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    //Toast.makeText(this, selectedImage.toString(), Toast.LENGTH_SHORT).show();
                    uriList.add(selectedImage);
                    Log.d("URI", selectedImage.toString());
                    imageAdapter.notifyDataSetChanged();
                }
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        AdapterView.AdapterContextMenuInfo aMenuInfo = (AdapterView.AdapterContextMenuInfo) menuInfo;
        //    ViewHolder tile = (ViewHolder) aMenuInfo.targetView;
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.set_delete_picture, menu);
    }


    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()) {
            case R.id.primary_menu_item:
                Toast.makeText(this, "Primary " + uriList.get((int) info.id) + (int) info.id,
                        Toast.LENGTH_SHORT).show();
                setPictureAsPrimary((int) info.id);
                return true;                    // (int)info.id
            case R.id.delete_menu_item:
                //Toast.makeText(this, "Delete " + uriList.get((int) info.id) + (int) info.id, Toast.LENGTH_SHORT).show();
                deletePicture((int) info.id);
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }

    public void setPictureAsPrimary(int id) {
        if (id != 0) {
            Uri tempUri = uriList.get(0);
            uriList.set(0, uriList.get(id));
            uriList.set(id, tempUri);
            Log.d("FUNCT primary", Integer.toString(id));
            imageAdapter.notifyDataSetChanged();
        }
        Log.d("FUNCT primary", Integer.toString(id));

    }

    public void deletePicture(int id) {
        uriList.remove(id);
        Log.d("FUNCT delete", Integer.toString(id));
        imageAdapter.notifyDataSetChanged();
    }

    public static List<Uri> getPhotos() {
        return uriList;
    }

}
